package clientDemo.demo;

import clientDemo.entity.MissionWork;
import clientDemo.entity.RuntimeParameter;
import clientDemo.service.MissionWorkService;
import clientDemo.service.RuntimeParameterService;
import clientDemo.service.impl.MissionWorkServiceImpl;
import clientDemo.service.impl.RuntimeParameterServiceImpl;

import java.util.List;

public class RuntimeParameterDemo {
    public static void main(String[] args) {
    	
    	/*
    	 * @describe 创建工作通过任务ID
    	 * @method   post请求
    	 * @url      http://ip:port/api/v2/missionWorks/{missionId}
    	 */
    	
    	//在机器人执行工作之前，需要在YOUIFleet中先创建好任务。我们需要先获取到任务的ID，才能创建调度工作。
        String missionId="2035ee2f-c965-11e9-b01f-b0416f03e8c3";
        MissionWorkService missionWorkService=new MissionWorkServiceImpl();
        MissionWork missionWork= missionWorkService.create(missionId);
      
       
     
        /*
         *  @describe  status:工作状态：
         *  WAIT:等待执行
         *  RUNNING: 执行中
         *  SUCCESS: 成功
         *  FAULT:失败
         *  PAUSE：已暂停
         *  SHUTDOWN：已取消
         *  WAITINPUT：等待输入
         */
        //持续监控missionWork状态，当status==WAITINPUT,开始更新动态参数
        while(true) {
        	String missionWorkId=missionWork.getId();
        	missionWork=missionWorkService.get(missionWorkId);
        	if("WAITINPUT".equals(missionWork.getStatus())) {
        		break;
        	}
        	try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
        }

       
        /*
    	 * @describe 查询等待输入的动态参数
    	 * @method   get请求
    	 * @url      http://ip:port/api/v2/missionWorks/{id}/emptyRuntimeParameters
    	 */
        String missionWorkId=missionWork.getId();
        List<RuntimeParameter> parameterList=missionWorkService.getRuntimeParameters(missionWorkId);

       
        /*
    	 * @describe 更新动态参数值 
    	 * @method   put请求
    	 * @url      http://ip:port/api/v2/runtimeParameters/{id}
    	 * @example  动态分配移动到移动到二维码动作 ,让机器人去 move_marker_id=10的点位 
    	 * 
    	 */
        RuntimeParameterService runtimeParameterService=new RuntimeParameterServiceImpl();
        for(RuntimeParameter runtimeParameter:parameterList) {
        	String parameterKey=runtimeParameter.getParameterKey();
        	if("move_marker_id".equals(parameterKey)) {
        		runtimeParameter.setParameterValue("10");
        		//动态分配去 marker_id=10,设置全部参数后，YOUIFleet系统会自动执行任务
        		runtimeParameter=runtimeParameterService.put(runtimeParameter);
        	}
        }
    }
}
